#ifndef EDITWIDGET_H
#define EDITWIDGET_H

#include <QGraphicsItem>
#include <QGraphicsView>

#include "Model/Geometry.h"
#include "Tool.h"
#include "Model/Light.h"

/**
 * \namespace Editor
 * \brief Editor-related classes : View items and Controllers
 *
 * This namespace contains the main Editor widget (EditWidget) and the different tools
 * used in it. It also contains different views items subclassing QGraphicsViewItem.
 */
namespace Editor {

class VertexItem : public QGraphicsItem {
public:
    VertexItem(Model::Vertex *vertex);
    
    QRectF boundingRect() const;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    
    Model::Vertex *vertex();
    const Model::Vertex *vertex() const;
    
    void setSelected(bool);
    bool selected();
    
private:
    Model::Vertex *_vertex;
    bool _selected;
};

class EdgeItem : public QGraphicsItem {    
public:
    EdgeItem(Model::Edge *edge);
    
    QRectF boundingRect() const;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    
    Model::Edge *edge();
    const Model::Edge *edge() const;
    
    void setSelected(bool);
    bool selected();
    
private:
    Model::Edge *_edge;
    bool _selected;
};
    
class EditWidget : public QGraphicsView {
    Q_OBJECT
    
public:
    EditWidget();
    
    void setMesh(Model::Mesh* mesh);
    Model::Mesh* mesh();
    
    void setLights(std::list<Model::Light>* l);
    std::list<Model::Light>& lights();
    
    void setTool(Tool* tool);
    Tool* tool();
    
    VertexItem* getNearVertex(QPoint);
    VertexItem* getNearVertex(QPointF);
    
    EdgeItem* getNearEdge(QPoint);
    EdgeItem* getNearEdge(QPointF);
    
    VertexItem* addVertex(QPointF coords);
    VertexItem* addVertex(Model::Vertex* vertex);
    
    EdgeItem* addEdge(Model::Vertex* start, Model::Vertex* end);
    EdgeItem* addEdge(Model::Edge* edge);
    
    void removeVertex(VertexItem* item);
    void removeEdge(EdgeItem* item);
    
protected:
    void mouseMoveEvent(QMouseEvent *event);
    void mousePressEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void keyPressEvent(QKeyEvent* event);
    
    Tool* _tool;
    Model::Mesh* _mesh;
    std::list<Model::Light>* _lights;
    QGraphicsPixmapItem* background;
    
    QList<EdgeItem*> edges;
    
public slots:
    void updateScene();
    void setBackgroundImage(const QString& url);
};

}

#endif // EDITWIDGET_H
